package com.itheima.wms.controller;

import com.itheima.wms.pojo.Area;
import com.itheima.wms.pojo.AreaDto;
import com.itheima.wms.pojo.AreaNumVo;
import com.itheima.wms.pojo.AreaPageRequest;
import com.itheima.wms.service.AreaService;
import com.itheima.wms.utils.PageBean;
import com.itheima.wms.utils.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/area")
@Slf4j
public class AreaController {

    private final AreaService areaService;

    @GetMapping("/pageDetail")
    public Result queryAreaPageByCondition(AreaPageRequest areaPageRequest) {
        PageBean pageBean = areaService.queryAreaPageByCondition(areaPageRequest);
        return Result.success(pageBean);
    }

    @GetMapping("/{id}")
    public Result queryAreaById(@PathVariable Long id) {
        Area area = areaService.queryAreaById(id);
        return Result.success(area);
    }

    @PostMapping
    public Result insertArea(@RequestBody Area area) {
        areaService.insertArea(area);
        return Result.success("");
    }

    @PutMapping
    public Result updateArea(@RequestBody Area area) {
        areaService.updateArea(area);
        return Result.success("");
    }


    @DeleteMapping
    public Result<String> delete(@RequestParam("ids[]") List<Long> ids) {
        boolean isEmpty = areaService.removeByIds(ids);
        if (isEmpty) {
            return Result.success("操作成功");
        }
        return Result.fail("删除失败");
    }

    @GetMapping("/list")
    public Result queryareaList(AreaDto areaPageRequest) {
        List<Area> areas = areaService.queryareaList(areaPageRequest);
        return Result.success(areas);
    }

    @GetMapping("/overview/{id}")
    public Result areaDetailOverViewById(@PathVariable Long id) {
        AreaNumVo areaNumVo = areaService.countIsUseOrFree(id);
        return Result.success(areaNumVo);
    }
}
